
#include "apdu_lib_dev_mgr.h"
#include "apdu_lib_common.h"

#include "../apdu.core/apdu.h"

#include "../../mk_lib/mk_auto_ptr.h"
#include "../../mk_lib/mk_utility.h"

#include <stdlib.h>
#include <string.h>
#include "../apdu.algo/mac.h"

extern int	g_sw;


int app_enum_device( char * parameter)
{
	int num = get_dev_mgr()->enum_device(parameter);
	app_destroy_removed_devs();

	return num;
}

HANDLE app_add_exist_device(char * devPath, HANDLE hCard)
{
	return get_dev_mgr()->add_exist_device(devPath, hCard);
}


int app_set_dev_name( HANDLE devHandle, char * devName )
{
	return get_dev_mgr()->set_dev_name(devHandle, devName);
}

char * app_get_dev_name(HANDLE devHandle)
{
	return get_dev_mgr()->get_dev_name(devHandle);
}


int app_update_dev_state( int * removed_num, int * new_num , char * parameter)
{
	int devNum = 0;
	return get_dev_mgr()->update_devices_state(&devNum, removed_num, new_num, parameter);
}

int app_get_dev_state( HANDLE devHandle, enmDevState * state )
{
	return get_dev_mgr()->get_dev_state(devHandle, state);
}

int app_set_dev_state(HANDLE devHandle, enmDevState state)
{
	return get_dev_mgr()->set_dev_state(devHandle, state);
}

int app_destroy_removed_devs()
{
	return get_dev_mgr()->destroy_removed_devices();
}

int app_delete_device(HANDLE devHandle)
{
	return get_dev_mgr()->delete_device(devHandle);
}

char * app_get_first_dev( HANDLE * devHandle, enmDevState state)
{
	* devHandle = get_dev_mgr()->get_first_device();
	if(*devHandle == NULL)
	{
		return NULL;
	}

	enmDevState tmpState;
	get_dev_mgr()->get_dev_state(*devHandle, &tmpState);
	if(state == devStateAny || 
		state == tmpState)
	{
		return get_dev_mgr()->get_dev_name(*devHandle);
	}

	return app_get_next_dev(devHandle, state);
}

char * app_get_next_dev( HANDLE * devHandle, enmDevState state )
{
	HANDLE handle = 0;
	handle = get_dev_mgr()->get_next_device();
	if(handle == NULL)
	{
		* devHandle = NULL;
		return NULL;
	}
	
	enmDevState tmpState;
	get_dev_mgr()->get_dev_state(handle, &tmpState);
	if(state == devStateAny || 
		tmpState == state)
	{
		* devHandle = handle;
		return get_dev_mgr()->get_dev_name(handle);
	}

	return app_get_next_dev(devHandle, state);
}



HANDLE app_get_dev_by_name( char * name )
{
	HANDLE devHandle = 0;
	if( 0 != get_dev_mgr()->get_dev_handle_by_name(name, &devHandle))
	{
		return 0;
	}

	return devHandle;
}

int app_connect_device(HANDLE devHandle )
{
	return get_dev_mgr()->open_device(devHandle);
}


int app_connect_device_by_name( char * name, HANDLE * devHandle )
{
	return get_dev_mgr()->open_device_by_name(name, devHandle);
}

int app_disconnect_device( HANDLE devHandle )
{
	return get_dev_mgr()->close_device(devHandle);
}

int app_get_dev_os_handle( HANDLE devHandle, HANDLE * osHandle )
{
	return get_dev_mgr()->get_os_handle(devHandle, osHandle);
}

int app_lock_device(HANDLE devHandle, long timeOut)
{
	return get_dev_mgr()->lock_device(devHandle, timeOut);
}

int app_unlock_device(HANDLE devHandle)
{
	return get_dev_mgr()->unlock_device(devHandle);
}

int app_begin_transaction(HANDLE devHandle)
{
	return get_dev_mgr()->begin_transaction(devHandle);
}

int app_end_transaction(HANDLE devHandle)
{
	return get_dev_mgr()->end_transaction(devHandle);
}
